package com.wnxy.controller;

import com.wnxy.domain.Order;
import com.wnxy.domain.Product;
import com.wnxy.service.IOrderService;
import com.wnxy.service.ProductService;
import com.wnxy.service.impl.OrderService2;
import com.wnxy.util.ResultConstant;
import com.wnxy.util.ResultVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Classname OrderController
 * @Description :
 * @Date 2021/2/23 18:12
 * @Created by Yeheng
 */
@RestController
public class OrderController {

    @Autowired
    ProductService service;

    @Autowired
    IOrderService iOrderService;

    @Autowired
    OrderService2 service2;

    @GetMapping("/order/findAll")
    public ResultVO findAll(){
        ResultVO vo = service.findAll();
        return vo;
    }
    @GetMapping("/order/get")
    public String get(){
        return System.currentTimeMillis()+"毫秒数";
    }

    @GetMapping("/order/create/{pid}/{num}")
    public ResultVO create(@PathVariable Integer pid,@PathVariable Integer num){
        try{
            ResultVO vo = service.findOne(pid);
            Product pro = new Product();
            BeanUtils.populate(pro,(Map)vo.getData());
            //扣库存
            service.reduceStock(pid,num);
            //保存订单
            Order order = new Order();
            order.setUid(1);
            order.setUsername("叶恒");
            order.setPid(pid);
            order.setPname(pro.getPname());
            order.setPrice(pro.getPrice());
            order.setNumber(num);
            iOrderService.save(order);
            return ResultVO.success(order);
        } catch(Exception e){
            e.printStackTrace();
            return new ResultVO(ResultConstant.OP_FAIL,e.getMessage());
        }
    }

    @GetMapping("/order/get2")
    public String get2(HttpServletRequest request){
        return System.currentTimeMillis()+"毫秒数"+service2.get()+request.getRequestURL();
    }
}
